<?php

namespace app\api\controller;

use app\admin\model\project\ProjectApply;
use app\api\model\project\ProjectApply as ProjectApplyModel;
use app\api\model\project\ProjectMedia;
use app\api\model\project\ProjectStaff as ProjectStaffModel;
use app\common\controller\Api;

//我的服务管理
class Projectstaff extends Api
{
    protected $noNeedRight = ['*'];
    protected $noNeedLogin = ['info'];

    //列表
    public function lists()
    {
        $order_status = input('status','all');

        $where = ['ps.user_id'=>$this->auth->id];

        switch ( $order_status ) {
            case "waitaudit"://待审核
                $where['pa.status'] = 0;
                break;
            case "serviceing"://服务中
                $where['pa.status'] = 1;
                break;
            case "pause"://已暂停
                $where['pa.status'] = 2;
                break;
            case "del"://已删除
                $where['pa.status'] = 3;
                break;
            case "refuse"://审核拒绝
                $where['pa.status'] = 4;
                break;
        }
        $project = ProjectApplyModel::alias('pa')
            ->join('project_staff ps','pa.staff_id = ps.id')
            ->join('project_category pc','pa.category_id = pc.id')
            ->join('project p','pa.project_id = p.id')
            ->where($where)
            ->field('pa.id,pa.staff_id,pa.is_recommend,pa.price,pa.unit,ps.name,pa.status,pa.project_id,pa.category_id,
            pc.name as category_name,p.name as project_name,ps.nickname')
            ->order('ps.zan_num desc')
            ->paginate(10, false, ['query' => request()->get()]);
        $this->success('',$project);

    }

    //删除
    public function del()
    {
        $id = input('id',0);
        $info = ProjectApplyModel::get($id);
        //$info->status = 3;
        //$res = $info->save();
        $res = $info->delete();
        $this->success('',$res);
    }

    //暂停
    public function pause()
    {
        $id = input('id',0);
        $info = ProjectApplyModel::get($id);
        $info->status = 2;
        $res = $info->save();

        $this->success('',$res);

    }

    //启动
    public function start()
    {
        $id = input('id',0);
        $info = ProjectApplyModel::get($id);
        $info->status = 1;
        $res = $info->save();

        $this->success('',$res);

    }

    //修改
    public function edit()
    {
        try {
            $model = new ProjectStaffModel();
            $param = input();
            //$param['user_id'] = $this->auth->id;
            $res = $model->edit($param);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('修改成功',$res);
    }

    //详情
    public function info()
    {

        $staff_id = input('staff_id',0);
        $info = ProjectStaffModel::where(['id'=>$staff_id])->field('id as staff_id,nickname,realname,gender,age,education,
        major,title,mobile,province,city,district,image,zan_num,collect_num,repost_num,province_id,city_id,district_id,name,
        region_limit,user_level,self_intro,place_province,place_city,place_district,place_province_id,place_city_id,
        place_district_id')->find();
        $info['comment_num'] = 0;
        $cert_list = ProjectMedia::where(['staff_id'=>$staff_id,'type'=>0])->select();
        $info['cert_list'] = $cert_list;
        $info['cert_img_count'] = ProjectMedia::where(['staff_id'=>$staff_id,'type'=>0])->count();

        $image_list = ProjectMedia::where(['staff_id'=>$staff_id,'type'=>1])->select();
        $info['image_list'] = $image_list;
        $info['image_count'] = ProjectMedia::where(['staff_id'=>$staff_id,'type'=>1])->count();

        $video_list = ProjectMedia::where(['staff_id'=>$staff_id,'type'=>2])->select();
        $info['video_list'] = $video_list;
        $info['video_count'] = ProjectMedia::where(['staff_id'=>$staff_id,'type'=>2])->count();

        $project_apply_list = ProjectApplyModel::where(['staff_id'=>$staff_id])->select();
        $apply_model = new ProjectApply();
        foreach ($project_apply_list as $k=>$v){
            $project_apply_list[$k]['work_day_text'] =  $apply_model->getWorkWeek($v['work_day']);
        }
        $info['project_apply_list'] = $project_apply_list;

        $this->success('',$info);
    }
}